﻿using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace WebSite.ViewModels
{
    public class TabsViewModel : IHtmlString
    {
        public string ControllerName { get; private set; }
        private readonly HtmlHelper m_HtmlHelper;
        private readonly List<TabItem> m_Tabs;

        public TabsViewModel(HtmlHelper htmlHelper, string controllerName)
        {
            ControllerName = controllerName;
            m_HtmlHelper = htmlHelper;
            m_Tabs = new List<TabItem>();
        }

        public IEnumerable<TabItem> Tabs { get { return m_Tabs; } }

        public TabsViewModel Add(string text, string imageUrl, IHtmlString content)
        {
            return Add(new TabItem(text, imageUrl, content));
        }

        public TabsViewModel Insert(int index, string text, string imageUrl, IHtmlString content)
        {
            return Insert(index, new TabItem(text, imageUrl, content));
        }

        public TabsViewModel Add(string text, string imageUrl, string content)
        {
            return Add(new TabItem(text, imageUrl, content));
        }

        public TabsViewModel Add(TabItem tabItem)
        {

            m_Tabs.Add(tabItem);
            return this;
        }

        public TabsViewModel Insert(int index, TabItem tabItem)
        {
            m_Tabs.Insert(index, tabItem);
            return this;
        }

        public TabsViewModel AddRange(IEnumerable<TabItem> tabItems)
        {
            m_Tabs.AddRange(tabItems);
            return this;
        }

        public IHtmlString Render()
        {
            return m_HtmlHelper.Partial("Tabs", this);
        }

        public string ToHtmlString()
        {
            return Render().ToHtmlString();
        }
    }

    public class TabItem
    {
        public string Text { get; private set; }
        public string ImageUrl { get; private set; }
        public IHtmlString Content { get; private set; }

        public TabItem(string text, string imageUrl, IHtmlString content)
        {
            Text = text;
            ImageUrl = imageUrl;
            Content = content;
        }

        public TabItem(string text, string imageUrl, string content)
            : this(text, imageUrl, new MvcHtmlString(content))
        {
        }
    }
}